<?php
session_start();
include("load-settings.php");

if(!isset($_SESSION['user']))
	header("Location: login.php");

$user = mysql_real_escape_string($_SESSION['user']);
$result = mysql_query("SELECT * FROM user WHERE id = $user");
$row = mysql_fetch_array($result);

if($row['type'] != 2)
	header("Location: home.php");
?>
<!doctype html>
<html>
<head>
  <meta charset="UTF-8" />
  <meta name="viewport" content="width=device-width, maximum-scale=1, initial-scale=1, user-scalable=0">
    <link rel="icon" href="http://www.veteranboosters.com/wp-content/uploads/2014/04/favicon4.png" type="image/x-icon"/>
    <link rel="shortcut icon" href="http://www.veteranboosters.com/wp-content/uploads/2014/04/favicon4.png" type="image/x-icon"/>
  <!-- Always force latest IE rendering engine or request Chrome Frame -->
  <meta content="IE=edge,chrome=1" http-equiv="X-UA-Compatible">

  <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Open+Sans:400,600,800">

  <!-- Use title if it's in the page YAML frontmatter -->
  <title>Veteran Boosters Order Tracking</title>


  <!--[if lt IE 9]>
  <script src="../../javascripts/vendor/html5shiv.js" type="text/javascript"></script>
  <script src="../../javascripts/vendor/excanvas.js" type="text/javascript"></script>
  <![endif]-->
<link rel="stylesheet" href="css/bootstrap.min.css" />
<link rel="stylesheet" href="css/font-awesome.css" />
<link rel="stylesheet" href="css/fullcalendar.css" />
<link rel="stylesheet" href="css/jquery.jscrollpane.css" />	
<link rel="stylesheet" href="css/unicorn.css" />
  <link href="stylesheets/application.css" media="screen" rel="stylesheet" type="text/css" />
  <script src="javascripts/application.js" type="text/javascript"></script>
</head>

<?php
	include("top.php");
?>

  <div class="container-fluid padded">
    <div class="row-fluid">

      <!-- Breadcrumb line -->

      <div id="breadcrumb">
	  <a href="/" title="" class="tip-bottom" data-original-title="Go to Home"><i class="fa fa-home"></i> Home</a>
				<a href="#">Members</a>
				<a href="#" class="current">View user history    </a>
			
            </div>
    </div>
  </div>

  <div class="container-fluid padded">
  <div class="row-fluid">
   <div class="col-lg-12">
      <div class="box">
         <div class="box-header">
            <span class="title">Enter user ID to search for</span>
         </div>
         <div class="box-content">
            <table class="table table-normal">
               <thead>
                  <tr>
                     <td>ID</td>
					 <td>Actions</td>
                  </tr>
               </thead>
               <tbody>
			   <tr><td><input type = "text" id = "id" /></td>
			   <td><input type="submit" class="btn btn-success btn-xs" value="Search" onclick = "fetch_history()"></td>
			   </tbody>
            </table>
         </div>
      </div>
   </div>
</div>
</div>
  <div class="container-fluid padded">
  <div class="row-fluid">
   <div class="col-lg-12">
      <div class="box">
         <div class="box-header">
            <span class="title">Order history</span>
         </div>
         <div class="box-content">
            <table class="table table-normal">
               <thead>
                  <tr>
                     <td>ID</td>
					 <td>Date</td>
                     <td>Paypal</td>
					 <td>Ordering IP</td>
					 <td>Summoner</td>
					 <td>Type</td>
					 <td>Amount</td>
					 <td>Details</td>
                  </tr>
               </thead>
               <tbody id = "order_history">
			   <?php
			   if(isset($_GET['id']))
			   {
				   $id = mysql_real_escape_string($_GET['id']);

					$returnArray = array();

					$result = mysql_query("SELECT * FROM order_record WHERE user = $id");

					while($row = mysql_fetch_array($result))
					{
						$row['order_type'] = "Boosting";
						array_push($returnArray, $row);
					}

					$result = mysql_query("SELECT * FROM duo_order WHERE user = $id");

					while($row = mysql_fetch_array($result))
					{
						$row['order_type'] = "Coaching";
						array_push($returnArray, $row);
					}

					$result = mysql_query("SELECT * FROM leveling_order WHERE user = $id");

					while($row = mysql_fetch_array($result))
					{
						$row['order_type'] = "Leveling";
						array_push($returnArray, $row);
					}

					$result = mysql_query("SELECT * FROM referral_order WHERE user = $id");

					while($row = mysql_fetch_array($result))
					{
						$row['order_type'] = "Referral";
						array_push($returnArray, $row);
					}

					$result = mysql_query("SELECT * FROM account_order WHERE user = $id");

					while($row = mysql_fetch_array($result))
					{
						$row['order_type'] = "Account";
						array_push($returnArray, $row);
					}

					function aasort (&$array, $key) {
						$sorter=array();
						$ret=array();
						reset($array);
						foreach ($array as $ii => $va) {
							$sorter[$ii]=$va[$key];
						}
						asort($sorter);
						foreach ($sorter as $ii => $va) {
							$ret[$ii]=$array[$ii];
						}
						$array=$ret;
					}

					aasort($returnArray,"timestamp");

					$x = 0;

					while(isset($returnArray[$x]))
					{
						echo "<tr><td>".$returnArray[$x]['id']."</td>";
						echo "<td>".date("d M Y g:i:sA", $returnArray[$x]['timestamp'])." PST</td>";
						echo "<td>".$returnArray[$x]['paypal']."</td>";
						echo "<td>".$returnArray[$x]['ip']."</td>";
						echo "<td>".$returnArray[$x]['summoner']."</td>";
						echo "<td>".$returnArray[$x]['order_type']."</td>";
						echo "<td>".$returnArray[$x]['payout']."</td>";
						if($returnArray[$x]['order_type'] == "Boosting")
							echo "<td><a class = 'btn btn-success btn-xs' href = 'order.php?id=".$returnArray[$x]['id']."'>Details</a></td></tr>";
						else if($returnArray[$x]['order_type'] == "Coaching")
							echo "<td><a class = 'btn btn-success btn-xs' href = 'duo_order.php?id=".$returnArray[$x]['id']."'>Details</a></td></tr>";
						else if($returnArray[$x]['order_type'] == "Leveling")
							echo "<td><a class = 'btn btn-success btn-xs' href = 'leveling_order.php?id=".$returnArray[$x]['id']."'>Details</a></td></tr>";
						else if($returnArray[$x]['order_type'] == "Referral")
							echo "<td><a class = 'btn btn-success btn-xs' href = 'referral_order.php?id=".$returnArray[$x]['id']."'>Details</a></td></tr>";
						$x++;
					}
			   }
				?>
			   </tbody>
            </table>
         </div>
      </div>
   </div>
</div>
</div>
<div class="container-fluid padded">
  <div class="row-fluid">
   <div class="col-lg-12">
      <div class="box">
         <div class="box-header">
            <span class="title">Login history</span>
         </div>
         <div class="box-content">
            <table class="table table-normal">
               <thead>
                  <tr>
                     <td>IP</td>
					 <td>Date</td>
                  </tr>
               </thead>
               <tbody id = "login_history">
			   <?php
			   if(isset($_GET['id']))
			   {
				   $id = mysql_real_escape_string($_GET['id']);

					$result = mysql_query("SELECT * FROM login_history WHERE user = $id");

					while($row = mysql_fetch_array($result))
					{
						echo "<tr><td>".$row['ip']."</td>";
						echo "<td>".date("d M Y g:i:sA", $row['timestamp'])." EST</td></tr>";
					}
			   }
				?>
			   </tbody>
            </table>
         </div>
      </div>
   </div>
</div>

</div>
<script>
function fetch_history ()
{
	fetch_logins();
	fetch_orders();
}
function fetch_logins ()
{
	var ajaxHandler = new XMLHttpRequest();

	ajaxHandler.onreadystatechange = function ()
	{
		if(ajaxHandler.readyState == 4)
		{
			document.getElementById("login_history").innerHTML = ajaxHandler.responseText;
		}
	}

	var form = new FormData();

	form.append("id", document.getElementById("id").value);

	ajaxHandler.open("POST", "fetch-logins.php");
	ajaxHandler.send(form);
}
function fetch_orders ()
{
	var ajaxHandler = new XMLHttpRequest();

	ajaxHandler.onreadystatechange = function ()
	{
		if(ajaxHandler.readyState == 4)
		{
			document.getElementById("order_history").innerHTML = ajaxHandler.responseText;
		}
	}

	var form = new FormData();

	form.append("id", document.getElementById("id").value);

	ajaxHandler.open("POST", "fetch-orders.php");
	ajaxHandler.send(form);
}
</script>
</div>
<?php
	include("bottom.php");
?>